package cn.lidar.impl;

import cn.lidar.entity.Date;
import cn.lidar.interfaces.ControlSearchService;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

@Service
public class ControlSearchImpl implements ControlSearchService {

  @Override
  public List<String> search(String siteName, Date start, Date end) {
    List<String> result = new ArrayList<>();
//        File dir = new File("C:" + File.separator + "LidarData" + File.separator + "bin");
    File dir = new File("D:" + File.separator + "LidarData" + File.separator + "bin" + File.separator + siteName);
    if (dir.exists() && dir.isDirectory()) {
      File[] files = dir.listFiles();
      if (files != null) {
        Pattern pattern = Pattern.compile("[0-9]+");    // 验证是否是纯数字
        int startDate = Integer.parseInt(String.format("%4d%02d%02d", start.getYear(), start.getMonth(), start.getDate()));
        int endDate = Integer.parseInt(String.format("%4d%02d%02d", end.getYear(), end.getMonth(), end.getDate()));
        for (File file : files) {   // 遍历当前文件夹
          if (file.isDirectory()) {   // 判断是不是文件夹
            String fileName = file.getName();   // 获取文件夹名字
            if (pattern.matcher(fileName).matches()) {  // 判断是不是纯数字
              int fn = Integer.parseInt(fileName);    // 转换成整型的纯数字
              if (fn >= startDate && fn <= endDate) {
                result.add(fileName);
              }
            }
          }
        }
      }
    }
    return result;
  }
}
